Skip to main content

Fetch API

Fetch API 提供了一个用于以编程方式执行网络调用的接口。您可以用于 fetch()以编程方式配置和执行 REST API。

示例:

1、获取请求

const questions = await fetch("https://opentdb.com/api.php?amount=10");
return questions.json();

2、发布请求

fetch("https://63772c9a5c477765121615ba.mockapi.io/users", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
name: "Alex",
email: "alex@appsmith.com",
}),
})
.then((response) => {
console.log("Success:", response.json());
})
.catch((error) => {
console.error("Error:", error);
});

3、上传文件

const formData = new FormData();
formData.append("file", FilePicker1.files[0]);

let response = await fetch("https://httpbin.org/post", {
method: "POST",
body: formData,
});